import re
from time import sleep

from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By

from base.base_page import BasePage
<<<<<<< HEAD
from utils.log import logger
=======
from common.log import logger
>>>>>>> 41acbbe (commit)


class DocumentsPage(BasePage):
    def search_input(self):
        return self.by_xpath('//android.widget.ImageView[@content-desc="搜索"]').click()

    def create_folder(self):
        return self.by_xpath(
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.ImageView[2]').click()

    def switch_view(self):
        return self.by_xpath(
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.ImageView[3]').click()

    def sort(self):
        return self.by_xpath(
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.ImageView[4]').click()

    # 导入功能
    def import_method(self):
        return self.by_xpath(
<<<<<<< HEAD
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.ImageView[5]').click()

    # 图片导入
    def import_pictures(self):
        # return self.by_id(self.HW_ENV + 'hor_tv1').click()
        ele = self.by_accessibility_id('相册图片')
        TouchAction(self.driver).tap(ele, 100, 100).perform()

    # PDF文件导入
    def import_pdf(self):
        # return self.by_id(self.HW_ENV + 'hor_tv2').click()
        ele = self.by_accessibility_id('PDF文档')
        TouchAction(self.driver).tap(ele, 100, 100).perform()
=======
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout[5]/android.widget.ImageView').click()

    # 图片导入
    def import_pictures(self):
        return self.by_id(self.HW_ENV + 'hor_tv1').click()
        # TouchAction(self.driver).tap(ele, 100, 100).perform()

    # PDF文件导入
    def import_pdf(self):
        return self.by_id(self.HW_ENV + 'hor_tv2').click()
        # TouchAction(self.driver).tap(ele, 100, 100).perform()
>>>>>>> 41acbbe (commit)

    # 完成
    def complete_btn(self):
        return self.by_accessibility_id('完成').click()

    def back_btn(self):
        return self.by_xpath(
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[1]/android.widget.ImageView[1]').click()

    def import_btn(self):
        return self.by_id(self.HW_ENV + 'btn_import').click()

    # 文件夹输入框
    def folder_input(self):
        return self.by_class_name('android.widget.EditText')

    # 全选
    def all_select_btn(self):
        return self.by_id(self.HW_ENV + 'txt_extension').click()

    # 移动
    def move_btn(self):
        return self.by_id(self.HW_ENV + 'click_move').click()

    # 移动页面新建文件夹
    def move_folder(self):
        return self.by_id(self.HW_ENV + 'barRightIcon').click()

    # 移动到此处
    def move_to(self):
        return self.by_id(self.HW_ENV + 'btn_complete').click()

    # 清除文件夹输入框内容
    def folder_cross(self):
        return self.by_id(self.HW_ENV + 'folder_cross').click()

    def sure_btn(self):
        return self.by_id(self.HW_ENV + 'sure').click()

    def confirm_btn(self):
        return self.by_accessibility_id('确定').click()

    # 点击PDF文件
    def choose(self, index):
        return self.by_xpath(
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout['
            + str(index) + ']/android.widget.RelativeLayout').click()

    def pdfs_choose(self, num):
        pdfs_elements = self.by_uiautomators('new UiSelector().resourceId("' + self.HW_ENV + 'item_checkbox")')
        num_of_page = len(pdfs_elements)
        page_num = int(num / num_of_page)
        remainder = num % num_of_page
<<<<<<< HEAD
        first_pdf = self.by_xpath(
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.RelativeLayout')
        last_pdf = self.by_xpath(
            '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[5]/android.widget.RelativeLayout')
=======
        first_pdf = self.by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.RelativeLayout')
        last_pdf = self.by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout[5]/android.widget.RelativeLayout')
>>>>>>> 41acbbe (commit)

        first_bound = first_pdf.get_attribute('bounds')
        first_bounds_list = re.findall(r'\d+', first_bound)
        last_bound = last_pdf.get_attribute('bounds')
        last_bounds_list = re.findall(r'\d+', last_bound)
        start_y = last_bounds_list[3]
        end_y = first_bounds_list[1]

        for i in range(0, page_num):
            for j in range(0, num_of_page):
                pdfs_elements[j].click()

            action = TouchAction(self.driver)
            action.long_press(x=500, y=start_y).move_to(x=500, y=end_y).release().perform()
        if remainder:
            for i in range(1, remainder + 1):
                self.choose(i)
